<!-- subpkg_consult/payment/payment.vue -->
<script lang="ts" setup>
  import { createOrderApi, orderPayApi, preOrderApi } from '@/api/consult'
  import { useConsultStore } from '@/stores/consult'
  import type { OrderPreData } from '@/types/consult'
  import { onLoad } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  import type { PatientItem } from '@/types/patient.d.ts'
  import { patientDetailApi } from '@/api/patient'
  // 省略前面小节的代码...

  const store = useConsultStore()

  const preOrderInfo = ref({} as OrderPreData)
  onLoad(async () => {
    const res = await preOrderApi({ type: store.type, illnessType: store.illnessType })
    preOrderInfo.value = res
  })

  const patientDetail = ref({} as PatientItem)
  onLoad(async () => {
    const res = await patientDetailApi(store.patientId)
    patientDetail.value = res
  })

  const payMethodRef = ref()

  // 1. 定义订单id变量
  const orderId = ref('')

  const onSelectPay = async () => {
    // 1.支付宝H5-支付主逻辑
    // 1.1 请求预支付订单信息, 让用户核对支付信息 或 收获地址之类✅
    // 1.2 请求创建订单 -  拉起支付方式✅
    // 1.3 调用后端接口, 获取订单id
    // 1.4 调用后端接口, 获取支付参数(网页版支付宝支付参数是一个url支付地址)
    // 1.5 通过window.location.href, 跳转到支付宝支付页面
    // 1.6 支付成功后, 支付宝通过回跳地址, 跳回自己的系统中

    const { id } = await createOrderApi({
      type: store.type,
      illnessType: store.illnessType,
      patientId: store.patientId,
      depId: store.depId,
      illnessDesc: store.illnessInfo.illnessDesc,
      illnessTime: store.illnessInfo.illnessTime,
      consultFlag: store.illnessInfo.consultFlag,
      pictures: store.illnessInfo.pictures,
    })

    store.clear()

    orderId.value = id

    payMethodRef.value.open()
  }

  const onCancelPay = async () => {
    const { confirm } = await uni.showModal({
      title: '关闭支付',
      content: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
      cancelText: '仍要关闭',
      cancelColor: '#848484',
      confirmText: '继续支付',
      confirmColor: '#16C2A3',
    })

    if (!confirm) {
      uni.reLaunch({
        url: '/pages/index/index',
      })
    }
  }

  // 2. 把支付的逻辑 放在父组件中
  const onPay = async (activeIndex: number) => {
    if (activeIndex === 0) {
      uni.showToast({
        title: '微信支付开发中',
        icon: 'none',
      })
      return
    }

    const res = await orderPayApi({
      orderId: orderId.value,
      paymentMethod: activeIndex + '',
      payCallback: 'http://localhost:5173/#/subpkg_consult/room/room', // 网页版支付宝 支付成功后回跳地址
    })

    window.location.href = res.payUrl
  }
</script>

<template>
  <scroll-page>
    <view class="payment-page">
      <uni-section
        title-font-size="32rpx"
        title-color="#121826"
        padding="30rpx"
        :title="`图文问诊 ${preOrderInfo.payment}元`"
      >
        <uni-list :border="false">
          <uni-list-item
            title="极速问诊"
            note="自动分配医生"
            thumb="/static/uploads/doctor-avatar.jpg"
            thumb-size="lg"
          />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="`-¥${preOrderInfo.couponDeduction}`"
          />
          <uni-list-item title="积分抵扣">
            <template #footer>
              <view class="uni-list-text-red">-¥{{ preOrderInfo.pointDeduction }}</view>
            </template>
          </uni-list-item>
          <uni-list-item title="实付款">
            <template #footer>
              <view class="uni-list-text-red">¥{{ preOrderInfo.actualPayment }}</view>
            </template>
          </uni-list-item>
        </uni-list>
      </uni-section>

      <view class="dividing-line"></view>

      <uni-section title-font-size="32rpx" title-color="#121826" padding="30rpx" title="患者资料">
        <uni-list :border="false">
          <uni-list-item title="患者信息">
            <template #footer>
              <view class="uni-list-text-gray">
                {{ patientDetail.name }} | {{ patientDetail.genderValue }} |
                {{ patientDetail.age }}岁
              </view>
            </template>
          </uni-list-item>
          <uni-list-item border title="病情描述" :note="store.illnessInfo.illnessDesc" />
        </uni-list>
      </uni-section>

      <!-- <view class="payment-agreement">
        <radio color="#20c6b2" value="1" />
        我已同意<text style="color: #20c6b2">支付协议</text>
      </view> -->
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <view class="total-amount">
        合计: <text class="number">¥{{ preOrderInfo.actualPayment }}</text>
      </view>

      <button class="uni-button" @click="onSelectPay">立即支付</button>
    </view>
  </scroll-page>

  <!-- 3. 监听支付行为 -->
  <custom-payment :amount="100" ref="payMethodRef" @on-cancel="onCancelPay" @on-confirm="onPay" />
</template>

<style lang="scss">
  @import './payment.scss';
</style>
